package org.school.streaming;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.DatagramSocket;
import java.net.ServerSocket;

import org.school.streaming.io.VideoStream;
import org.school.streaming.net.RequestType;
import org.school.streaming.net.State;
import org.school.streaming.net.server.Server;

/**
 * @authors Jeffrey Goudzwaard, Davin Terrizzi
 * @version 1.0
 * @date 18-4-2013
 */

/**
 * Start-up class of streaming server
 * 
 * @author Davin
 * 
 */
public class BootServer {

	/**
	 * Start-up method of streaming server
	 * 
	 * @param args
	 *            First value holds port to let the server listen on
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Server server = Server.getServer();

		// Initialize GUI
		server.showFrame();

		// Set port to listen on and open socket
		int port = Integer.parseInt(args[0]);
		ServerSocket listenSocket = new ServerSocket(port);
		server.setSocket(listenSocket.accept());
		listenSocket.close();

		// Get IP address of client from socket and set it
		server.setClientIPAddress(server.getSocket().getInetAddress());

		server.setServerState(State.INIT);

		// Initialize streamreader and streamwriter
		server.setBufferedReader(new BufferedReader(new InputStreamReader(server.getSocket().getInputStream())));
		server.setBufferedWriter(new BufferedWriter(new OutputStreamWriter(server.getSocket().getOutputStream())));

		RequestType requestType;

		boolean done = false;

		// Start-up loop - runs on first start and waits until client connection
		// is made
		// When client connection is made, set ServerState to READY, initialize
		// VideoStream and initialize socket for sending video information
		while (!done) {
			requestType = server.parseRequest();

			if (requestType == RequestType.SETUP) {
				done = true;

				server.setServerState(State.READY);
				System.out.println("New RTSP state: READY");

				server.sendResponse();

				server.setVideo(new VideoStream(server.getVideoFileName()));

				server.setDatagramSocket(new DatagramSocket());
			}
		}

		// Main loop - checks client request and sends right response to client + sets new server state
		// Also closes starts playpack timer and/or ServerSocket 
		while (true) {
			requestType = server.parseRequest();

			if (requestType == RequestType.PLAY && server.getServerState() == State.READY) {
				server.sendResponse();
				server.getTimer().start();
				server.setServerState(State.PLAYING);
				System.out.println("New RTSP state: PLAYING");
			} else if (requestType == RequestType.PAUSE) {
				server.sendResponse();
				server.getTimer().stop();
				server.setServerState(State.READY);
				System.out.println("New RTSP state: READY");
			} else if (requestType == RequestType.TEARDOWN) {
				server.sendResponse();
				server.getTimer().stop();
				server.getDatagramSocket().close();
				server.getSocket().close();

				System.exit(0);
			}
		}
	}
}
